package com.jy.adscm.stock.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.dealer.bean.Dealer;
import com.jy.adscm.dealer.mapper.DealerMapper;
import com.jy.adscm.order.bean.JyProduct;
import com.jy.adscm.order.mapper.JyProductMapper;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.stock.bean.JyInStorage;
import com.jy.adscm.stock.bean.JyOutStorage;
import com.jy.adscm.stock.bean.JyRemainingStorage;
import com.jy.adscm.stock.mapper.JyInStorageMapper;
import com.jy.adscm.stock.mapper.JyOutStorageMapper;
import com.jy.adscm.stock.service.JyRemainingStorageService;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zyl
 * @Date 2019/1/25 -14:23
 */
@Service
public class JyRemainingStorageServiceImpl implements JyRemainingStorageService {
    @Autowired
     JyInStorageMapper  jyInStorageMapper;
    @Autowired
    JyOutStorageMapper jyOutStorageMapper;
   @Autowired
    JyProductMapper jyProductMapper;
   @Autowired
    DealerMapper dealerMapper;

    @Override
    public List<JyRemainingStorage> selectAll(Token token) {
        List<JyRemainingStorage> jyRemainingStorages =new ArrayList<>();
        List<JyInStorage> jyDealerhaveProduct = new ArrayList<>();
        //这个厂家的人
        if (token.getUser().getUnitId()==0){
            Wrapper<JyInStorage> wrapper =new EntityWrapper<>();
          //查询一共有多少经销商
            List<JyInStorage> jydealers = jyInStorageMapper.queryDealer();
            for (JyInStorage jyInStorage : jydealers) {
                jyDealerhaveProduct = jyInStorageMapper.queryDealerHaveProduct(jyInStorage.getDealer());
                for (JyInStorage jy : jyDealerhaveProduct) {
                    JyRemainingStorage jyRemainingStorage =new JyRemainingStorage();
                    jyRemainingStorage.setDealer(jyInStorage.getDealer());
                    jyRemainingStorage.setProduct(jy.getProduct());
                    jyRemainingStorage.setType(jy.getType());
                    jyRemainingStorage.setTypeNum(jy.getTypeNum());
                    jyRemainingStorages.add(jyRemainingStorage);
                }
            }

        }else {
            //查询这个经销商 订单列表
           jyDealerhaveProduct = jyInStorageMapper.queryDealerHaveProduct(token.getUser().getUnitId().toString());
            for (JyInStorage jy : jyDealerhaveProduct) {
                JyRemainingStorage jyRemainingStorage =new JyRemainingStorage();
                jyRemainingStorage.setDealer(token.getUser().getUnitId().toString());
                jyRemainingStorage.setProduct(jy.getProduct());
                jyRemainingStorage.setType(jy.getType());
                jyRemainingStorage.setTypeNum(jy.getTypeNum());
                jyRemainingStorages.add(jyRemainingStorage);
            }
        }

        //遍历列表查询出每个商品  入库总数 与出库总数
        for (JyRemainingStorage jyRemainingStorage : jyRemainingStorages) {

            int intmep=0;
            Wrapper<JyInStorage> jyInStoragewrapper =new EntityWrapper<>();
            jyInStoragewrapper.eq("dealer",jyRemainingStorage.getDealer());
            jyInStoragewrapper.eq("product",jyRemainingStorage.getProduct());
            List<JyInStorage> jyInStorages = jyInStorageMapper.selectList(jyInStoragewrapper);
            for (JyInStorage jyInStorage : jyInStorages) {
                if (AdscmBaseUtils.getInstance().isNotEmpty( jyInStorage.getInstorageNum(),null)){
                    intmep= intmep+jyInStorage.getInstorageNum();
                }
            }
            jyRemainingStorage.setInStorageNum(String.valueOf(intmep));

            int outtmep=0;
            Wrapper<JyOutStorage> jyOutStorageWrapper =new EntityWrapper<>();
            jyOutStorageWrapper.eq("dealer",jyRemainingStorage.getDealer());
            jyOutStorageWrapper.eq("product",jyRemainingStorage.getProduct());
            List<JyOutStorage> jyOutStorages = jyOutStorageMapper.selectList(jyOutStorageWrapper);
            for (JyOutStorage jyOutStorage : jyOutStorages) {
                if(AdscmBaseUtils.getInstance().isNotEmpty(jyOutStorage.getNum(),null)){
                    outtmep=outtmep+jyOutStorage.getNum();
                }
            }
            jyRemainingStorage.setOutStorageNum(String.valueOf(outtmep));
            int i = intmep - outtmep;
            jyRemainingStorage.setRemainingStorage(String.valueOf(i));

            if (AdscmBaseUtils.getInstance().isNotEmpty(jyRemainingStorage.getProduct(),null)){
                JyProduct jyProduct = jyProductMapper.selectById(jyRemainingStorage.getProduct());
                jyRemainingStorage.setProduct(jyProduct.getName());
                jyRemainingStorage.setType(jyProduct.getType());
                jyRemainingStorage.setTypeNum(jyProduct.getTypeNum());
            }
            if (AdscmBaseUtils.getInstance().isNotEmpty(jyRemainingStorage.getDealer(),null)){
                Dealer dealer = dealerMapper.selectById(jyRemainingStorage.getDealer());
                jyRemainingStorage.setDealer(dealer.getName());
            }
        }


        return jyRemainingStorages;
    }
}
